
const CONFIG = require('./config.js')
const API_BASE_URL = 'https://www.janvipiana.com/zhufeng'

const request = (url, needSubDomain, method, data) => {
  let _url = API_BASE_URL + url
  return new Promise((resolve, reject) => {
    wx.request({
      url: _url,
      method: method,
      data: data,
      header: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
        'tokenid': wx.getStorageSync('token')
      },
      success(request) {
        resolve(request.data)
      },
      fail(error) {
        reject(error)
      },
      complete(aaa) {
        // 加载完成
      }
    })
  })
}

/**
 * 小程序的promise没有finally方法，自己扩展下
 */
Promise.prototype.finally = function (callback) {
  var Promise = this.constructor;
  return this.then(
    function (value) {
      Promise.resolve(callback()).then(
        function () {
          return value;
        }
      );
    },
    function (reason) {
      Promise.resolve(callback()).then(
        function () {
          throw reason;
        }
      );
    }
  );
}

module.exports = {
  request,
  getziyuan: (url) => {
    return API_BASE_URL + url
  },
  queryMobileLocation: (data) => {
    return request('/common/mobile-segment/location', false, 'get', data)
  },
  dictlist: (typeid) => {
    return request('/sys/dictlist.do', false, 'get', { typeid})
  },
  queryConfigBatch: (ids) => {
    return request('/sysmanager/getconfig.do', true, 'get', { ids })
  },
  scoreRules: (data) => {
    return request('/score/send/rule', true, 'post', data)
  },
  scoreSign: (token) => {
    return request('/score/sign', true, 'post', {
      token
    })
  },
  scoreSignLogs: (data) => {
    return request('/score/sign/logs', true, 'post', data)
  },
  scoreTodaySignedInfo: (token) => {
    return request('/score/today-signed', true, 'get', {
      token
    })
  },
  scoreExchange: (number, token) => {
    return request('/score/exchange', true, 'post', {
      number,
      token
    })
  },
  scoreLogs: (data) => {
    return request('/score/logs', true, 'post', data)
  },
  shareGroupGetScore: (referrer, encryptedData, iv) => {
    return request('/score/share/wxa/group', true, 'post', {
      referrer,
      encryptedData,
      iv
    })
  },
  kanjiaList: (data) => {
    return request('/baobei/qdlist.do', true, 'post', data)
  },
  kanjiaSet: (id) => {
    return request('/kanjia/getinfo.do', true, 'get', { id })
  },
  kanjiaJoin: (id) => {
    return request('/kanjia/kanjia.do', true, 'post', {
      id
    })
  },
  kanjiaDetail: (id) => {
    return request('/kanjia/getkanjia.do', true, 'get', {
      id
    })
  },
  kanjiaHelp: (id) => {
    return request('/kanjia/kanjiadet.do', true, 'post', {
      id
    })
  },
  kanjiaHelpDetail: (id) => {
    return request('/kanjia/getkanjiadet.do', true, 'get', {id
    })
  },
  getToken: (token) => {
    return request('/user/getuser.do', true, 'get', {
      token
    })
  },
  addTempleMsgFormid: (data) => {
    return request('/template-msg/wxa/formId', true, 'post', data)
  },
  sendTempleMsg: (data) => {
    return request('/template-msg/put', true, 'post', data)
  },
  wxpay: (data) => {
    return request('/pay/wx/wxapp.do', true, 'post', data)
  },
  alipay: (data) => {
    return request('/pay/alipay/semiAutomatic/payurl', true, 'post', data)
  },
  login: (data) => {
    return request('/user/wxapp/login.do', true, 'post',data)
  },
  register: (data) => {
    return request('/user/wxapp/register/complex', true, 'post', data)
  },
  updateref: (refereeId) => {
    return request('/user/updateref.do', true, 'post', { refereeId})
  },
  banners: (typeid) => {
    return request('/cms/cmslist.do', true, 'get', {typeid})
  },
  getcms: (id) => {
    return request('/cms/get.do', true, 'get', { id })
  },
  goodsCategory: () => {
    return request('/shop/goods/category/all', true, 'get')
  },
  goods: (data) => {
    return request('/baobei/qdlist.do', true, 'post', data)
  },
  sygoods: () => {
    return request('/baobei/sylist.do', true, 'post', {})
  },
  typeTree: () => {
    return request('/type/gettype.do', true, 'post', {})
  },
  goodsDetail: (id) => {
    return request('/baobei/getfull.do', true, 'get', {
      id
    })
  },
  goodsPrice: (data) => {
    return request('/shop/goods/price', true, 'post', data)
  },
  goodsReputation: (data) => {
    return request('/shop/goods/reputation', true, 'post', data)
  },
  coupons: (status) => {
    return request('/card/list.do', true, 'get', {status})
  },
  couponDetail: (id) => {
    return request('/discounts/detail', true, 'get', {
      id
    })
  },
  myCoupons: (data) => {
    return request('/discounts/my', true, 'get', data)
  },
  fetchCoupons: (data) => {
    return request('/discounts/fetch', true, 'post', data)
  },
  noticeList: (data) => {
    return request('/notice/list', true, 'post', data)
  },
  noticeDetail: (id) => {
    return request('/notice/detail', true, 'get', {
      id
    })
  },
  addAddress: (data) => {
    return request('/user/saveaddr.do', true, 'post', data)
  },
  updateAddress: (data) => {
    return request('/user/saveaddr.do', true, 'post', data)
  },
  deleteAddress: (id, token) => {
    return request('/user/shipping-address/delete', true, 'post', {
      id,
      token
    })
  },
  queryAddress: (token) => {
    return request('/user/useraddr.do', true, 'get', {
      token
    })
  },
  defaultAddress: (token) => {
    return request('/user/useraddr.do', true, 'get', {
      token
    })
  },
  addressDetail: (id) => {
    return request('/user/getaddr.do', true, 'get', {
      id
    })
  },
  pingtuanSet: (goodsId) => {
    return request('/shop/goods/pingtuan/set', true, 'get', {
      goodsId
    })
  },
  pingtuanOpen: (goodsId, token) => {
    return request('/shop/goods/pingtuan/open', true, 'post', {
      goodsId,
      token
    })
  },
  pingtuanList: (goodsId) => {
    return request('/shop/goods/pingtuan/list', true, 'get', {
      goodsId
    })
  },
  videoDetail: (videoId) => {
    return request('/media/video/detail', true, 'get', {
      videoId
    })
  },
  bindMobile: (data) => {
    return request('/user/wxapp/bindmobile.do', true, 'post', data)
  },
  updateuser: (data) => {
    return request('/user/updateuser.do', true, 'post', data)
  },
  userDetail: (token) => {
    return request('/user/getuser.do', true, 'get', {
      token
    })
  },
  userRef: (token) => {
    return request('/user/getrefuser.do', true, 'get', {
      token
    })
  },
  userByCode: (code) => {
    return request('/user/getuserbycode.do', true, 'get', {
      code
    })
  },
  uservip: () => {
    return request('/user/getvip.do', true, 'get', {})
  },

  userAmount: (token) => {
    return request('/user/getzhyue.do', true, 'get', {
      token
    })
  },
  userDjq: (token) => {
    return request('/user/djqlist.do', true, 'get', {
      token
    })
  },

  userZfxx: (id) => {
    return request('/order/getzffs.do', true, 'get', {
      id
    })
  },
  userwdz: () => {
    return request('/zhang/userfanli.do', true, 'get', {
      
    })
  },
  vipPay: (code) => {
    return request('/inout/savevip.do', true, 'get', {code})
  },
  savein: (jine) => {
    return request('/inout/savein.do', true, 'get', {jine})
  },
  //购物车保存
  carCreate: (data) => {
    return request('/car/create.do', true, 'post', data)
  },
  //购物车删除
  carDel: (ids) => {
    return request('/car/del.do', true, 'post', {ids:ids})
  },
  //购物车列表
  carlist: () => {
    return request('/car/list1.do', true, 'post', {})
  },
  orderCreate: (data) => {
    return request('/order/create.do', true, 'post', data)
  },
  orderList: (data) => {
    return request('/order/list.do', true, 'post', data)
  },
  orderDetail: (id) => {
    return request('/order/detail.do', true, 'get', {
      id
    })
  },
  preorder: (data) => {
    return request('/order/preorder.do', true, 'post', data)
  },
  orderDelivery: (orderId, token) => {
    return request('/order/delivery', true, 'post', {
      orderId,
      token
    })
  },
  orderReputation: (data) => {
    return request('/order/reputation', true, 'post', data)
  },
  orderClose: (id) => {
    return request('/order/close.do', true, 'post', {
      id
    })
  },
  //确认收货
  ordershouhuo: (id) => {
    return request('/order/shouhuo.do', true, 'post', {
      id
    })
  },
  orderPay: (data) => {
    return request('/order/pay.do', true, 'post', data)
  },
  orderStatistics: (token) => {
    return request('/order/statistics', true, 'get', {
      token
    })
  },
  // 提现
  withDrawApply: (jine) => {
    return request('/inout/saveout.do', true, 'post', {
      jine
    })
  },
  // 提现,需要后台审核
  withDrawApply2: (data) => {
    return request('/inout/saveout2.do', true, 'post', data)
  },
  
  province: () => {
    return request('/sys/province.do', false, 'get')
  },
  choosecity: (code) => {
    return request('/sys/city.do', false, 'get', {
      code
    })
  },
  choosearea: (code) => {
    return request('/sys/area.do', false, 'get', {
      code
    })
  },
  cashLogs: (data) => {
    return request('/zhang/userzhanglist.do', true, 'post', data)
  },
  rechargeSendRules: () => {
    return request('/user/recharge/send/rule', true, 'get')
  },
  payBillDiscounts: () => {
    return request('/payBill/discounts', true, 'get')
  },
  payBill: (data) => {
    return request('/payBill/pay', true, 'post', data)
  },
  fxApply: (token, name, mobile) => {
    return request('/saleDistribution/apply', true, 'post', { token, name, mobile })
  },
  fxApplyProgress: (token) => {
    return request('/saleDistribution/apply/progress', true, 'get', { token })
  },
  fxMembers: (data) => {
    return request('/user/fensilist.do', true, 'post', data)
  },
  fxShouyi: () => {
    return request('/user/getshouyi.do', true, 'post', {})
  },
  fxCommisionLog: (data) => {
    return request('/zhang/userfanlilist.do', true, 'post', data)
  },
  wxaQrcode: (data) => {
    return request('/qrcode/wxa/unlimit.do', true, 'post', data)
  },
  uploadFile: (tempFilePath) => {
    const uploadUrl = API_BASE_URL + '/' + CONFIG.subDomain + '/file/upfiles.do'
    return new Promise((resolve, reject) => {
      wx.uploadFile({
        url: uploadUrl,
        filePath: tempFilePath,
        name: 'upfile',
        header: {
          'tokenid': wx.getStorageSync('token')
        },
        success(res) {
          resolve(JSON.parse(res.data))
        },
        fail(error) {
          reject(error)
        },
        complete(aaa) {
          // 加载完成
        }
      })
    })
  },
  refundApply: (data) => {
    return request('/order/saveback.do', true, 'post', data)
  },
  choujianglist: () => {
    return request('/cj/cjlist.do', true, 'post', {})
  },
  choujiangindex: () => {
    return request('/cj/sysj.do', true, 'post', {})
  },
  refundApplyDetail: (id) => {
    return request('/order/orderdetail.do', true, 'get', {
      id
    })
  },
  refundApplyCancel: (token, orderId) => {
    return request('/order/refundApply/cancel', true, 'post', {
      token,
      orderId
    })
  },
  cmsCategories: () => {
    return request('/cms/category/list', true, 'get', {})
  },
  cmsArticles: (data) => {
    return request('/cms/news/list', true, 'post', data)
  },
  cmsArticleDetail: (id) => {
    return request('/cms/news/detail', true, 'get', {
      id
    })
  },
}